import { BoidSkill } from "../../config/boid_def";
import { CollisionTag, DEL_BULLET_TIME, GetBuffType} from "../../config/play_def";
// 敌方子弹
cc.Class({
    extends: cc.Component,

    properties: {
        move:false,                 // 移动开关
        damege:1,                   // 伤害
        _damege:1,                  // 初始伤害
        order_line:0,               // 对应列
        dir:cc.Vec2.ZERO,           // 方向
    },
    initInfo(info){
        this.initComponent(info)
        this.move = true            // 开始移动
        this.damege = info.damege
        this._damege = info.damege
        this.style = info.style
        this.speed = info.speed
        this.pool = 'debullet'
        this.order_line = info.order_line
        if(!this.order_line){
            this.clearInfo()
        }
        if(info.overtime >= 0){
            this.overtime = info.overtime
        }else{
            this.overtime = DEL_BULLET_TIME
        }
    },
    // 组件
    initComponent(info){
        // 模型基础赋值
        this.model = this.node.getComponent(sp.Skeleton)
        // he.log(info.style.url,info.style.type.name)
        let bulletSpine = he.load.get(info.style.url,info.style.type)
        this.model.skeletonData = bulletSpine
        if(info.style && info.style.skin){
            this.model.defaultSkin = info.style.skin
            this.model.setSkin(info.style.skin)
        }
        this.playAttack()
        this.model.setCompleteListener((trackEntry) => {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            this.listenAniTag(animationName)
        });
    },
    // 动画监听
    listenAniTag(tag){ 
        let breakS = tag.indexOf('_')
        let aniType = tag
        if(breakS >= 0){
            aniType = tag.substring(0,breakS)
        }
        switch (aniType) {
            case 'hit':
                this.clearInfo()
            break
        }
    },
    // 攻击技能
    attackSkill() {
        if (this.info && this.info.skill) {
            let skill_list = this.info.skill.split("#")
            let skill_param_list
            if (this.info.skillParam)
                skill_param_list = this.info.skillParam.split("#")
            for (let i = 0; i < skill_list.length; i++) {
                let skill_param
                if (skill_param_list)
                    skill_param = skill_param_list[i]
                switch (parseInt(skill_list[i])) {
                    case BoidSkill.A_Down_Role:
                        let delRole
                        for (let tkey in Tower2Way) {
                            if (Tower2Way[tkey].indexOf(this.order_line) >= 0 && game_main.control.getPlaceStatus(CardType.ROLE, tkey)) {
                                delRole = tkey
                                break
                            }
                        }
                        if (!delRole) {
                            for (let tIndex = 0; tIndex < TowerList.length; tIndex++) {
                                let name = TowerList[tIndex].x + '_' + TowerList[tIndex].y + '_' + TowerList[tIndex].z
                                if (game_main.control.getPlaceStatus(CardType.ROLE, name)) {
                                    delRole = name
                                    break
                                }
                            }
                        }
                        if (delRole) {
                            game_main.control.towerScript[delRole].delTowerRole()
                        } else {
                            game_main.data.beAttacked(1)
                        }
                        // 先判断临近的炮塔
                        // 再判断所有炮塔
                        break
                    case BoidSkill.C_Goon:
                        let backMon = game_main.boidsManager.addMonsterBottom(this.order_line, skill_param)
                        let backMonScript = backMon.getComponent("monster")
                        let saveDamegeBuff = {
                            key: BoidBuff.DamegeSave,
                            value: this.damege,
                        }
                        backMonScript.HP = this.HP
                        backMonScript.addBuff(saveDamegeBuff)
                        let currentExLife = this.getBuffList(BoidBuff.EXLife)
                        for(let exLifeIndex = 0; exLifeIndex < currentExLife.length;exLifeIndex ++){
                            backMonScript.addBuff(currentExLife[i])
                        }
                        break
                }
            }
        }
    },
    attack(param){
        this.move = false
        this.attackSkill()
        gg.data.hurtHp(this.damege)
        this.playHit()
    },
    // 攻击动画
    playAttack(){
        this.model.defaultAnimation = 'attack'
        this.model.setAnimation(0,'attack',true)
    },
    // 受击动画
    playHit(){
        this.model.setAnimation(0,'hit',false)
    },
    // 清除子弹
    clearInfo(){
        this.effect = []
        this.speed = 1      // 速度归位
        this._damege = 0    // 伤害归位
        this.damege = 0     // 伤害归位
        this.count = 0      // 帧数归零
        this.move = false   // 关闭移动
        this.order_line = null  
        this.dir = cc.Vec2.ZERO
        this.skill = null
        this.skillParam = null

        this.model.skeletonData = null
        this.model.setCompleteListener(null)
        gg.bulletManager.delBullet(this.orderId)
        // he.update
    },
    // 飞行
    fly(dt){},
    // 飞行检测
    update(dt){
        if(this.move  && !gg.data.pause && gg.data.status){
            this.fly(dt)
            if(this.overtime > 0){
                this.count += dt
                if(this.count >= this.overtime){
                    this.clearInfo()
                }
            }
        }
    },
});
